/*
 * main.c
 *
 *  Created on: Mar 12, 2011
 *      Author: Sharks group.
 */

#include"../common/gba.h"
#include"../common/screenmode.h"
#include"../common/dma.h"
#include"../common/sprite.h"
#include "../common/interrupt.h"
#include "../common/timers.h"
#include "../common/keypad.h"

#include "../image-data/ca2.raw.c"
#include "../image-data/master.pal.c"
#include "../image-data/person.raw.c"
#include "../image-data/cacon.raw.c"
#include "../image-data/heart.raw.c"

#include "Sound.c"
#include "gba_common_utils.c"
#include "math.c"
#include "Fish.h"
#include "Person.h"
#include "GameMode.c"

#define OAMDataBMP ((u16*)0x6014000)//use the bitmap background (0x6014000 - 0x6018000)

#define NUMBER_OF_FISH 7
Fish fishes[NUMBER_OF_FISH];


void setUpFish(){
	fishes[0] = fish_new(220, 120, 2, 0);
	fishes[1] = fish_new(280, 60, 1, 0);
	fishes[2] = fish_new(260, 60, 2, 1);
	fishes[3] = fish_new(250, 40, 2, 1);
	fishes[4] = fish_new(250, 30, 4, 1);
	fishes[5] = fish_new(250, 100, 2, 1);
	fishes[6] = fish_new(250, 130, 3, 1);
}

void interrupt_handler(){
	//enable global interrupt
	REG_IME = 0;

	if(REG_IF & INT_TIMER2){
		REG_IF |= INT_TIMER2;
		//Make Fish swim.
		fishSwimming();

	}

	if(REG_IF & INT_KEYBOARD){
		switch (getMode()) {
			case PLAY_MODE:
				setUpFish();
				setUpPerson();
				loadBG();
				InitializeSprites();
				fishSwimming();
				break;
			case WELCOME_MODE:
				if (!((*KEYS) & KEY_A)) {
					setMode(CREDIT_MODE);
					if(getMode() ==CREDIT_MODE){
						loadCreditScreen();
					}
				}
				if (!((*KEYS) & KEY_START)) {
					setMode(PLAY_MODE);
				}
				break;
			case CREDIT_MODE:
				if (!((*KEYS) & KEY_START)) {
					setMode(PLAY_MODE);
				}
				break;
			default:
				setMode(PLAY_MODE);
				break;
		}
	}
	//disable global interrupt
	REG_IME =1;
}

void fishSwimming() {
	int i;
	//Do change position to make FISH swim.
	for(i =0; i < NUMBER_OF_FISH; ++i) {
		fishes[i].x -= fishes[i].speech;
		//reset to started point when fish reach the LEFT screen.
		recheckFishPosition(&fishes[i]);
	}
}

/**
 * Copy all objects into OAM memory.
 */
void refreshOAM(){
	int i;
	Fish fish;
	for(i =0; i < NUMBER_OF_FISH; ++i) {
		fish = fishes[i];
		sprites[i].attribute0 = SQUARE | COLOR_256 | fish.y;
		if(fish.danger){
			sprites[i].attribute1 = SIZE_32 | fish.x;
			sprites[i].attribute2 = 512;//char number
		}else{
			sprites[i].attribute1 = SIZE_16 | fish.x;
			sprites[i].attribute2 = 512+32+32;
		}
	}

	sprites[i].attribute0 = SQUARE | COLOR_256 | person.y;
	sprites[i].attribute1 = SIZE_32 | person.x;
	sprites[i].attribute2 = 512+32;

	//Draw blood.
	int heartx = 220;
	int hearty = 5;
	int loop;
	for (loop =0; loop < person.blood; ++loop) {
		++i;
		sprites[i].attribute0 = SQUARE | COLOR_256 | hearty;
		sprites[i].attribute1 = SIZE_16 | heartx;
		sprites[i].attribute2 = 512 + 32 + 32 + 8;
		heartx -= 20;
	}
	//clear blood.
	for (loop = 0; loop < LIMIT_BLOOD_MAX_PERSON - person.blood; ++loop) {
		++i;
		sprites[i].attribute0 = 0;
		sprites[i].attribute1 = 0;
		sprites[i].attribute2 = 0;
	}
	gba_cpy16(OAM, (u16*)sprites, 128*4);//SIZE_OF_SPRITE * NUMBER_OF_ATTRIBUTE
}

void clearSprite(){
	int i;
	Fish fish;
	for(i =0; i < NUMBER_OF_FISH; ++i) {
		fishes[i].x = 300;
	}
	person.x=300;
//	InitializeSprites();
//	gba_cpy16(OAM, (u16*)sprites, 128*4);
	WaitForVsync();

}
//Check collision
void checkCollision(){
	int i;
	for(i=0; i < NUMBER_OF_FISH; i++){
		if(verifyOverlapRectangle(fishes[i].x, fishes[i].y,
								  fishes[i].x+16, fishes[i].y+16,
								  person.x, person.y,
								  person.x +16, person.y + 16)){
			//handle: fish die, person die, or something else here.
			if(fishes[i].danger){
				changeFishSpeech(&fishes[i], 1);
				changePowerPerson(&person, -1);//decrease blood and speech.

//				PlaySound(&soundDead);//Play sound
				channelSound();

				if (person.blood == LIMIT_BLOOD_MIN_PERSON) {
					//show man hinh thua
					setMode(LOOSE_MODE);
					clearSprite();
					loadOverScreen();


				}

			}else{
				changePowerPerson(&person, 1);//increase blood and speech.
				if(person.blood == LIMIT_BLOOD_MAX_PERSON) {
					//show man hinh thang
					loadWinScreen();
					clearSprite();
					setMode(WIN_MODE);
				}
			}
			resetFishPosition(&fishes[i]);//just make fish go to start point.
		}
	}
}


void main (void){
	REG_DISPCNT = MODE_4 | BG2_ENABLE | OBJ_ENABLE | OBJ_MAP_1D;
	//Interrupt timer enable.
	REG_IME = 1;
	REG_P1CNT = KEY_INTERRUPT | KEY_START | KEY_A;
	REG_IE = REG_IE | INT_TIMER2 | INT_KEYBOARD;
	REG_INTERUPT = (u32)interrupt_handler;
	REG_TM2D = 60000;
	REG_TM2CNT = TIME_ENABLE | TIME_FREQUENCY_256 | TIME_IRQ_ENABLE;

	gba_cpy16( OAMDataBMP, (u16*)ca2_Bitmap,  32*32/2);
	gba_cpy16( OAMDataBMP+512, (u16*)person_Bitmap,  32*32/2);
	gba_cpy16( OAMDataBMP+512 + 512, (u16*)cacon_Bitmap,  16*16/2);
	gba_cpy16( OAMDataBMP+512 + 512 + 128, (u16*)heart_Bitmap,  16*16/2);
	gba_cpy16( OBJPaletteMem, (u16*)master_Palette, sizeof(master_Palette)/sizeof(u16));

	loadWelcomeScreen();

	setMode(WELCOME_MODE);
	while(1){
		if(getMode()==PLAY_MODE){
			WaitForVsync();
			handlePersonByKeyboard();
			checkCollision();
			refreshOAM();
		}
	}
}
